package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.domain.OtherRobotInfo;
import com.ruoyi.system.domain.OtherRoomJobConfig;
import com.ruoyi.system.domain.vo.OtherRobotUpdateReqVo;
import com.ruoyi.system.mapper.OtherRobotInfoMapper;
import com.ruoyi.system.mapper.OtherRobotRoomJobConfigMapper;
import com.ruoyi.system.service.IOtherRobotInfoService;
import com.ruoyi.system.service.IOtherRobotRoomJobConfigService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

@Service
@DataSource(value = DataSourceType.SLAVE)
public class OtherRobotRoomJobConfigServiceImpl extends ServiceImpl<OtherRobotRoomJobConfigMapper, OtherRoomJobConfig> implements IOtherRobotRoomJobConfigService {

    /**
     * 机器人服务器地址
     */
    @Value(value = "${robot.url}")
    private String robotUrl;

    @Resource
    private OtherRobotRoomJobConfigMapper otherRobotRoomJobConfigMapper;

    @Resource
    private RestTemplate restTemplate;

    @DataSource(value = DataSourceType.SLAVE)
    public List<OtherRoomJobConfig> selectRoomJobConfigList() {
        return otherRobotRoomJobConfigMapper.selectList(new LambdaQueryWrapper<OtherRoomJobConfig>());
    }


    @DataSource(value = DataSourceType.SLAVE)
    public OtherRoomJobConfig selectRoomJobConfigByRoomId(String roomId) {
        return otherRobotRoomJobConfigMapper.selectOne(new LambdaQueryWrapper<OtherRoomJobConfig>().eq(OtherRoomJobConfig::getRoomID, roomId));
    }


    @DataSource(value = DataSourceType.SLAVE)
    public List<OtherRoomJobConfig> selectRoomJobConfigByRoomIdS(List<String> roomId) {
        return otherRobotRoomJobConfigMapper.selectList(new LambdaQueryWrapper<OtherRoomJobConfig>().in(OtherRoomJobConfig::getRoomID, roomId));
    }


    public Boolean updateRoomJobConfig(OtherRobotUpdateReqVo otherRoomJobConfig) {
        String url = robotUrl + "/changeSetting";
        ResponseEntity<OtherRobotUpdateReqVo> otherRobotUpdateReqVoResponseEntity = restTemplate.postForEntity(url, otherRoomJobConfig, OtherRobotUpdateReqVo.class);
        HttpStatus status = otherRobotUpdateReqVoResponseEntity.getStatusCode();
        return status == HttpStatus.OK;
    }


}
